$(document).ready(function() {
    $('#checkallTinTuc').click(function(event) {  //on click 
        var me = $(this);
        if(this.checked) { 
            
            $('.ckTinTuc').each(function() { //loop through each checkbox
                $(this).parent('span').addClass("checked");
                $(this).attr('checked',true);   
            });
        }else{
             $('.ckTinTuc').each(function() { //loop through each checkbox
                $(this).parent('span').removeClass("checked");
                $(this).attr('checked',false);    
            });
            //.attr('checked',true);        
        }
    });

    //-------------------------------------------------------------------------------------
    // Show popover confirm delete many items
    //--------------------------------------------------------------------------------------

    $('#popoverDelete1Item').dialog({
        autoOpen: false, 
        modal: true,
        width: 400,
        buttons: {
           "Ok": function() {
                var id = $(this).data('id');
                deleteTinTucAjax(id);

            },
            "Cancel": function() {
                $( this ).dialog( "close" );
            }
        }
    });


    //-------------------------------------------------------------------------------------
    // Show popover confirm delete all
    //--------------------------------------------------------------------------------------

    $("#b_popup_4").dialog({
        autoOpen: false, 
        modal: true,
        width: 400,
        buttons: {                            
            "Ok": function() {
                var mArrId = $(this).data('listId');
                deleteTinTucAjaxArray(mArrId, mArrId.length -1); // delete bằng đệ qui
                $( this ).dialog( "close" );
            },
            "Cancel": function() {
                $( this ).dialog( "close" );
            }
        }
    });

    $("#xoaTinDuocChon").click(function(){
        // list danh sach id duoc chon
        var mArrId = [];
        $('.ckTinTuc').each(function() { //loop through each checkbox
            if($(this).attr('checked') =="checked" || $(this).attr('checked')==true) {
                mArrId.push($(this).attr("value"));
            }
            
        });
        if(mArrId.length ==0) {
            $("#dialogThongBao").dialog({autoOpen: true, show: "blind", hide: "explode",  modal: true});
        } else {
            $("#b_popup_4").data('listId', mArrId).dialog('open');
        }
    });

    $(".deleteTintuc .delete").click(function(){
            var id = $(this).attr('value');
            $("#popoverDelete1Item").data('id', id).dialog('open');
        }
    );

});

function deleteTinTucAjax(id) {
    $.ajax({ 
        type: "POST", 
        url:  base_url+ "tintuc/deleteTinTuc",  
        data: { TinTuc_id: id},  
        success: function(theResponse) {            
            if(theResponse =="true") {
                window.location = base_url + "tintuc";
            } else {
                alert("Delete không thành công, xin liên hệ với nhóm lập trình");
            }
            $( this ).dialog( "close" );
        }  
    });
}

function deleteTinTucAjaxArray(mArrId, index) {
    if(index<0) {
        window.location = base_url + "tintuc";
        return;
    }

    $.ajax({ 
        type: "POST", 
        url:  base_url+ "tintuc/deleteTinTuc",  
        data: { TinTuc_id: mArrId[index]},  
        success: function(theResponse) {
            if(theResponse =="true") {
                deleteTinTucAjaxArray(mArrId, index - 1);
            } else {
                alert("Delete không thành công, xin liên hệ với nhóm lập trình");
            }
            $( this ).dialog( "close" );
        }  
    });
}
